//
//  Sky72FacebookView.m
//  Sky72
//
//  Created by ladaka on 13. 10. 4..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "Sky72FacebookView.h"
#import <AddressBook/AddressBook.h>

@interface Sky72FacebookView ()

@end

@implementation Sky72FacebookView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // 친구 추가하기 탭 인식 설정
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addFriends)];
    tapRecognizer.numberOfTapsRequired = 1;
    [self.tvPostFriend addGestureRecognizer:tapRecognizer];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBtnPostPhoto:nil];
    [self setTfPostMessage:nil];
    [self setTvPostFriend:nil];
    [super viewDidUnload];
}
- (IBAction)onClickPost:(id)sender {

}

#pragma mark - UI Behavior 
- (void)addFriends {
//    if (FBSession.activeSession.isOpen) {
//        FBFriendPickerViewController *friendPicker = [[FBFriendPickerViewController alloc] init];
//        
//        // Set up the friend picker to sort and display names the same way as the
//        // iOS Address Book does.
//        
//        // Need to call ABAddressBookCreate in order for the next two calls to do anything.
//        ABAddressBookRef addressBook = ABAddressBookCreate();
//        ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();
//        ABPersonCompositeNameFormat nameFormat = ABPersonGetCompositeNameFormat();
//        
//        friendPicker.sortOrdering = (sortOrdering == kABPersonSortByFirstName) ? FBFriendSortByFirstName : FBFriendSortByLastName;
//        friendPicker.displayOrdering = (nameFormat == kABPersonCompositeNameFormatFirstNameFirst) ? FBFriendDisplayByFirstName : FBFriendDisplayByLastName;
//        
//        [friendPicker loadData];
//        [friendPicker presentModallyFromViewController:self
//                                              animated:YES
//                                               handler:^(FBViewController *sender, BOOL donePressed) {
//                                                   if (donePressed) {
//                                                       self.selectedFriends = friendPicker.selection;
//                                                       [self updateWithFriends];
//                                                   }
//                                               }];
//        CFRelease(addressBook);
//    } else {
//        // if not logged in, give the user the option to log in
//        [self presentLoginSettings];
//    }
    
    if (FBSession.activeSession.isOpen) {
        FBFriendPickerViewController *friendPickerController = [[FBFriendPickerViewController alloc] init];
        friendPickerController.title = @"Pick Friends";
        [friendPickerController loadData];
        
        // Use the modal wrapper method to display the picker.
        [friendPickerController presentModallyFromViewController:self animated:YES handler:
         ^(FBViewController *sender, BOOL donePressed) {
             
             if (!donePressed) {
                 return;
             }
             
             NSString *message;
             
             if (friendPickerController.selection.count == 0) {
                 message = @"<No Friends Selected>";
             } else {
                 
                 NSMutableString *text = [[NSMutableString alloc] init];
                 
                 self.selectedFriends = friendPickerController.selection;
                 
                 // we pick up the users from the selection, and create a string that we use to update the text view
                 // at the bottom of the display; note that self.selection is a property inherited from our base class
                 for (id<FBGraphUser> user in friendPickerController.selection) {
                     if ([text length]) {
                         [text appendString:@", "];
                     }
                     [text appendString:user.name];
                 }
                 message = text;
             }
             
             [[[UIAlertView alloc] initWithTitle:@"You Picked:"
                                         message:message
                                        delegate:nil
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil]
              show];
         }];
    } else {
        [self presentLoginSettings];
    }
    
}

- (void)updateWithFriends {
    int friendCount = self.selectedFriends.count;
    if (friendCount > 2) {
        // Just to mix things up, don't always show the first friend.
        id<FBGraphUser> randomFriend = [self.selectedFriends objectAtIndex:arc4random() % friendCount];
        self.tvPostFriend = [NSString stringWithFormat:@"%@ and %d others",
                           randomFriend.name,
                           friendCount - 1];
    } else if (friendCount == 2) {
        id<FBGraphUser> friend1 = [self.selectedFriends objectAtIndex:0];
        id<FBGraphUser> friend2 = [self.selectedFriends objectAtIndex:1];
        self.tvPostFriend = [NSString stringWithFormat:@"%@ and %@",
                           friend1.name,
                           friend2.name];
    } else if (friendCount == 1) {
        id<FBGraphUser> friend = [self.selectedFriends objectAtIndex:0];
        self.tvPostFriend.text = friend.name;
    }
}

-(void)presentLoginSettings {
    if (self.settingsViewController == nil) {
        self.settingsViewController = [[FBUserSettingsViewController alloc] init];
#ifdef __IPHONE_7_0
        if ([self.settingsViewController respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
            self.settingsViewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
        }
#endif
        self.settingsViewController.delegate = self;
    }
    
    [self.navigationController pushViewController:self.settingsViewController animated:YES];
}
@end
